<?php
//echo 'Class SWP';
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
class SWP
{
	public static function is_plugin_installed($name)
	{
		return is_plugin_active($name);
	}
	
	public static function has_wordpress_seo()
	{
		return is_plugin_active('wordpress-seo/wp-seo.php');
	}
	
	public static function has_disqus()
	{
		return is_plugin_active('disqus-comment-system/disqus.php');
	}
	public static function the_charset()
	{
		bloginfo('charset');
	}
	
	public static function the_pingback()
	{
		bloginfo('pingback_url');
	}
	
	public static function the_post_tag()
	{
	?>
		<div class="post-tag">
			<span class="tag-list">
			<?php the_tags('', '', ' '); ?>
			</span>
		</div>
	<?php
	}
	
	/*=====================================================================*/
	/* Name: SPagination
	/* Author: Sau Hi
	/* Comment: This WordPress Pagination Function is created by Sau Hi.
	/* Please keep this comment if you want to use this function.
	/* Email: contact@sauhi.com
	/* Website: http://sauhi.com/en/
	/* Version: 1.1
	/* Last update: July 28, 2013
	/*=====================================================================*/
	public static function the_paginate($title = 'Pages')
	{
		$currentPage = null;
		$totalPage = null;
		global $wp_query;
		$currentPage = intval(get_query_var('paged'));
		if(empty($currentPage))
		{
			$currentPage = 1;
		}		
		$totalPage = intval(ceil($wp_query->found_posts / intval(get_query_var('posts_per_page'))));
		if($totalPage <= 1)
		{
			return '';
		}
		$paginateResult = '<div id="sPaginate" class="spaginate"><div class="spaginate-inner"><span class="spaginate-title">'.$title.':</span>';
		
		if ($currentPage > 1)
		{
			$paginateResult .= '<a href="'.get_pagenum_link($currentPage - 1).'">&laquo;</a>';
		}		
		$paginateResult .= self::the_paginate_list(1, $totalPage, $currentPage);
		if ($currentPage < $totalPage)
		{
			$paginateResult .= "<a href='" . get_pagenum_link($currentPage + 1) . "' class='spaginate-next'>&raquo;</a>";
		}
		$paginateResult .= "</div></div>";	
		echo $paginateResult;	
		return $paginateResult;
	}

	public static function the_paginate_list($intStart, $totalPage, $currentPage)
	{
		$pageHidden = '<span class="spaginate-hidden">...</span>';
		$linkResult = "";
		$hiddenBefore = false;
		$hiddenAfter = false;
		for ($i = $intStart; $i <= $totalPage; $i++)
		{
			if($currentPage === intval($i))
			{
				$linkResult .= '<span class="spaginate-current">'.$i.'</span>';
			}
			else if(($i <= 6 && $currentPage < 10) || $i == $totalPage || $i == 1 || $i < 4 || ($i <= 6 && $totalPage <= 6) || ($i > $currentPage && ($i <= ($currentPage + 2))) || ($i < $currentPage && ($i >= ($currentPage - 2))) || ($i >= ($totalPage - 2) && $i < $totalPage))
			{
				$linkResult .= '<a class="spaginate-link" href="'.get_pagenum_link($i).'">'.$i.'</a>';
				if($i <= 6 && $currentPage < 10)
				{
					$hiddenBefore = true;
				}
			}
			else
			{
				if(!$hiddenBefore)
				{
					$linkResult .= $pageHidden;
					$hiddenBefore = true;
				}
				else if(!$hiddenAfter && $i > $currentPage)
				{
					$linkResult .= $pageHidden;
					$hiddenAfter = true;
				}
			}
		}
		return $linkResult;
	}
	
	
	
	public static function the_head_seo()
	{
	?>
		<!-- SWPF SEO -->
		<!-- / SWPF SEO -->
	<?php
	}
	
	public static function get_the_theme_folder_name()
	{
		return wp_get_theme()->get( 'TextDomain' );
	}
	
	public static function get_the_site_name()
	{
		return get_bloginfo('name');
	}
	
	public static function the_site_name()
	{
		echo self::get_the_site_name();
	}
	
	public static function get_the_site_description()
	{
		return get_bloginfo('description');
	}
	
	public static function the_archive_list($num = 24)
	{
		wp_get_archives( array( 'type' => 'monthly', 'limit' => $num ) );
	}
	
	public static function the_site_description()
	{
		echo self::get_the_site_description();
	}
	
	public static function the_theme_folder_name()
	{
		echo self::get_the_theme_folder_name();
	}
	
	public static function get_the_site_uri()
	{
		return home_url();
	}
	
	public static function the_site_uri()
	{
		echo self::get_the_site_uri();
	}
	
	public static function the_list_category()
	{
		wp_list_categories('orderby=name&hierarchical=0&title_li=');
	}
	
	public static function get_the_style_uri()
	{
		return get_stylesheet_uri();
	}
	
	public static function the_style_uri()
	{
		echo self::get_the_style_uri();
	}
	
	public static function the_404_template()
	{
	?>
<section class="not-found">
<img class="not-found-image" src="<?php SWPF::the_theme_image_uri('404.png'); ?>" />
<div class="section-text page-width">
<h2>Trang này không tồn tại</h2>
<p>Có thể đã bị xóa hoặc là bị lỗi. Hãy liên hệ với admin nhờ giúp đỡ hoặc...</p>
<a class="ask-button" href="<?php self::the_site_uri(); ?>">Trở về trang chủ</a>
</div>
</section>
	<?php
	}
	
	public static function the_header($args = null)
	{
		//echo 'Function the header';
		$defaults = array('html_lang' => 'vi-VN', 'wpseo_installed' => false, 'favicon' => home_url('/')."favicon.ico");
		$args = wp_parse_args( $args, $defaults );
		extract( $args, EXTR_SKIP );
		?>
<!DOCTYPE html>
<html>
<head>
<meta charset="<?php self::the_charset(); ?>">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<title><?php wp_title( '-', true, 'right' ); ?></title>
<link rel="stylesheet" id="swp-style-css" href="<?php SWPF::the_style_uri('swp-style'); ?>" type="text/css" media="all" />
<link rel="stylesheet" id="<?php self::the_theme_folder_name(); ?>-style-css" href="<?php self::the_style_uri(); ?>" type="text/css" media="all" />
<?php if(SPHP::is_ie()) : ?>
<link rel="stylesheet" id="swp-style-ie-css" href="<?php SWPF::the_style_uri('swp-ie'); ?>" type="text/css" media="all" />
<?php endif; ?>
<link rel="profile" href="http://gmpg.org/xfn/11">
<link rel="pingback" href="<?php self::the_pingback(); ?>">
<?php add_to_head(); ?>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<link rel="alternate" type="application/rss+xml" title="<?php SWP::the_site_name(); ?> &raquo; Feed" href="<?php SWP::the_site_uri(); ?>/feed" />
<link rel="alternate" type="application/rss+xml" title="<?php SWP::the_site_name(); ?> &raquo; Comments Feed" href="<?php SWP::the_site_uri(); ?>/comments/feed/" />
<link rel="Shortcut Icon" href="<?php self::the_site_uri(); ?>/favicon.ico" type="image/x-icon" />
<?php
if(self::has_wordpress_seo())
{
}
else
{
	self::the_head_seo();
}
?>		
<?php wp_head(); ?>
</head>
<body <?php body_class(); ?>>
<div id="Wrap" class="wrap">
<?php
	}
	
	public static function the_post_category_link()
	{
		$categories = get_the_category();
		$separator = ', ';
		$output = '';
		if($categories)
		{
			foreach($categories as $category)
			{
				$output .= '<a href="'.get_category_link( $category->term_id ).'" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) ) . '">'.$category->cat_name.'</a>'.$separator;
			}
		echo trim($output, $separator);
		}
	}
	
	public static function get_the_post_comment()
	{
		return  get_comments_number();
	}
	
	public static function the_post_comment()
	{
		echo self::get_the_post_comment();
	}
	
	public static function the_post_author_link()
	{
		the_author_posts_link();
	}
	
	public static function the_post_info()
	{
	?>
		<div class="byline">
			<span itemprop="author" class="post-author">
				<?php self::the_post_author_link(); ?>							
			</span>
			<meta content="<?php echo get_the_date('Y-m-d'); ?>" itemprop="datePublished">
			<meta content="<?php echo get_the_modified_date('Y-m-d'); ?>" itemprop="dateModified">
			<span title="<?php echo get_the_date('Y-m-d'); ?>" class="post-date"><?php echo get_the_date('d/m/Y'); ?></span>
			<div itemprop="keywords" class="post-cats">
				<?php self::the_post_category_link(); ?>
			</div>
			<meta content="UserComments:<?php self::the_post_comment(); ?>" itemprop="interactionCount">
			<a rel="nofollow" href="<?php the_permalink(); ?>#comments" class="num-comments-link"><span class="num-comments"><?php comments_number( '0', '1', '%' ); ?></span> bình luận</a>
		</div>
	<?php
	}
	
	public static function the_page_view()
	{
	?>
		<article itemtype="http://schema.org/Article" itemscope="" class="post-box" id="post-<?php the_ID(); ?>">
			<header class="headline-area">
				<h1 itemprop="name" class="headline">
					<a rel="bookmark" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
				</h1>
			</header>
			<div itemprop="articleBody" class="post-content page-content">
			<?php the_content(); ?>
			</div>
		</article>
		<?php self::the_comment_box(); ?>
	<?php	
	}
	
	public static function the_single_post()
	{
	?>
		<article itemtype="http://schema.org/Article" itemscope="" class="post-box" id="post-<?php the_ID(); ?>">
			<header class="headline-area">
				<h1 itemprop="name" class="headline">
					<a rel="bookmark" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
				</h1>
				<?php self::the_post_info(); ?>
			</header>
			<div itemprop="articleBody" class="post-content post-single">
			<?php the_content(); ?>
			</div>
		</article>
		<?php self::the_post_tag(); ?>
		<?php self::the_comment_box(); ?>
	<?php
	}
	
	public static function get_the_thumbnail()
	{
		$result = "";
		if(has_post_thumbnail())
		{
			$result = get_the_post_thumbnail();
		}
		else
		{
			global $post, $posts;
			$first_img = '';		
			$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
			$first_img = $matches[1][0];
			if(empty($first_img))
			{
				//$result = '<img alt="'.$post->post_title.'" src="'.GetThemeImage('noimage.png').'" />';
			}
			else
			{
				$result = "<img src=\"".$first_img."\" alt=\"".$post->post_title."\" />";
			}
		}
		return $result;
	}
	
	public static function the_thumbnail()
	{
		echo self::get_the_thumbnail();
	}
	
	
	
	public static function get_the_post_excerpt()
	{
		$str = get_the_excerpt();
		$key = SPHP::get_char_from_last($str, 10);
		if($key == '[&hellip;]' || $key == '[...]')
		{
			$str = trim(SPHP::string_replace($str, '[&hellip;]', '')).'&hellip;';
		}
		return trim($str);
	}
	
	public static function the_post_excerpt()
	{
		echo self::get_the_post_excerpt();
	}
	
	public static function the_comment_box()
	{
		if(self::has_disqus())
		{
			echo '<div id="comments" class="comment-box">';
		}
		comments_template();
		if(self::has_disqus())
		{
			echo '</div>';
		}
	}
	
	public static function get_max_tag_count($tags)
	{
		$ltag = array_pop($tags);
		$kq = $ltag->count;
		foreach($tags as $tag)
		{
			if($tag->count > $kq)
			{
				$kq = $tag->count;
			}
		}
		return $kq;
	}

	public static function the_hot_tag_list($num = 100)
	{
		$tags = get_tags();
		if ($tags)
		{					
			$tags = self::get_hot_tag($tags);
			$count = 0;
			foreach ($tags as $tag)
			{
				echo '<li class="tag-'.$tag->term_id.' tag-item"><a href="'.get_tag_link($tag->term_id).'" title="'.sprintf(__( "Tìm bài viết với từ khóa %s" ),$tag->name ).'">'.self::get_the_sub_string($tag->name, 30).'</a></li>';
				$count++;
				if($count>$num)
				{
					break;
				}
			}
		}
	}
	
	public static function the_list_page()
	{
		wp_list_pages('title_li=');
	}
	
	public static function get_the_sub_string($str, $len, $more = '...', $encode = 'utf-8')
	{
		return self::get_sub_string($str, $len, $more, $encode);
	}
	
	public static function the_sub_string($str, $len, $more = '...', $encode = 'utf-8')
	{
		echo self::get_the_sub_string($str, $len, $more, $encode);
	}

	public static function get_sub_string($str, $len, $more = '...', $encode = 'utf-8')
	{
		if ($str == "" || $str==NULL || is_array($str) || strlen($str) <= $len)
		{
			return $str;
		}	
		$str = mb_substr($str, 0, $len, $encode);
		if ($str != "")
		{
			if (!substr_count($str, " "))
			{
				$str .= $more;
				return $str;
			}
			while(strlen($str) && ($str[strlen($str)-1] != " "))
			{
				$str = mb_substr($str, 0, -1, $encode);
			}
			$str = mb_substr($str, 0, -1, $encode);
			$str .= $more;
		}
		$str = preg_replace("/[[:blank:]]+/", " ", $str);
		$str = preg_replace("/<br[^>]+\>/i", " ", $str);
		$str = strip_tags($str);
		return $str;
	}
	
	public static function get_hot_tag($tags, $number=0)
	{
		$maxcount = self::get_max_tag_count($tags);
		$kq = array();	
		while($maxcount>$number)
		{
			foreach($tags as $tag)
			{
				if($tag->count == $maxcount)
				{
					array_push($kq, $tag);
				}
			}
			$maxcount--;
		}	
		return $kq;
	}
	
	public static function the_one_home_post()
	{
	?>
	<div itemtype="http://schema.org/Article" itemscope="" class="post-box" id="post-<?php the_ID(); ?>">
		<header class="headline-area">
			<h2 itemprop="name" class="headline">
				<a rel="bookmark" href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
			</h2>
			<?php self::the_post_info(); ?>
		</header>
		<div itemprop="articleBody" class="post-content">
			<div class="thumb-image">
				<?php SWP::the_thumbnail(); ?>
			</div>
			<div class="preview-text">
				<?php self::the_post_excerpt(); ?>
				<p>
					<a class="more-link" href="<?php the_permalink(); ?>">Xem bài đầy đủ</a>
				</p>
			</div>
		</div>
	</div>
	<?php
	}
	
	public static function enable_admin_bar()
	{
		show_admin_bar(true);
	}

	public static function disable_admin_bar()
	{
		show_admin_bar(false);
	}
	
	public static function the_comment_template( $args = null )
	{
		if ( post_password_required() )
		{
			return;
		}
		$defaults = array('avatar_size' => 60, 'display_title' => false);
		$args = wp_parse_args( $args, $defaults );
		extract( $args, EXTR_SKIP );
?>

<div id="comments" class="comment-box">

	<?php if ( have_comments() ) : ?>
	<?php if($display_title) : ?>
		<h2 class="comments-title">
<?php printf( _nx( 'Hiện đang có tổng cộng %1$s bình luận', 'Hiện đang có tổng cộng %1$s bình luận', get_comments_number(), 'comments title', self::get_the_theme_folder_name() ), number_format_i18n( get_comments_number() ), '<span>' . get_the_title() . '</span>' ); ?>
		</h2>
	<?php endif; ?>
		<ol class="comment-list">
			<?php
				wp_list_comments( array('style' => 'ol', 'short_ping' => true, 'avatar_size' => $avatar_size) );
			?>
		</ol>

		<?php
			if ( get_comment_pages_count() > 1 && get_option( 'page_comments' ) ) :
		?>
		<nav class="navigation comment-navigation" role="navigation">
			<h1 class="screen-reader-text section-heading"><?php _e( 'Comment navigation', self::get_the_theme_folder_name() ); ?></h1>
			<div class="nav-previous"><?php previous_comments_link( __( '&larr; Older Comments', self::get_the_theme_folder_name() ) ); ?></div>
			<div class="nav-next"><?php next_comments_link( __( 'Newer Comments &rarr;', self::get_the_theme_folder_name() ) ); ?></div>
		</nav>
		<?php endif; ?>

		<?php if ( ! comments_open() && get_comments_number() ) : ?>
		<p class="no-comments"><?php _e( 'Bình luận đã được đóng.' , self::get_the_theme_folder_name() ); ?></p>
		<?php endif; ?>

	<?php endif; ?>

	<?php comment_form(array('title_reply' => 'Gửi bình luận', 'title_reply_to' => 'Gửi bình luận tới', 'label_submit' => 'Đăng bình luận')); ?>

</div>
	<?php
	}
	
	public static function the_footer()
	{
		?>
		</div>
		<?php add_script_to_footer(); ?>
		</body>
		</html>
		<?php
	}
}

function add_to_head()
{
	do_action('add_to_head');
}

function add_script_to_footer()
{
	do_action('add_script_to_footer');
}

function my_comment_callback($comment, $args, $depth) {
		$GLOBALS['comment'] = $comment;
		extract($args, EXTR_SKIP);

		if ( 'div' == $args['style'] ) {
			$tag = 'div';
			$add_below = 'comment';
		} else {
			$tag = 'li';
			$add_below = 'div-comment';
		}
?>
		<<?php echo $tag ?> <?php comment_class(empty( $args['has_children'] ) ? '' : 'parent') ?> id="comment-<?php comment_ID() ?>">
		<?php if ( 'div' != $args['style'] ) : ?>
		<div id="div-comment-<?php comment_ID() ?>" class="comment-body">
		<?php endif; ?>
		<div class="comment-author vcard">
		<?php if ($args['avatar_size'] != 0) echo get_avatar( $comment, $args['avatar_size'] ); ?>
		<?php printf(__('<cite class="fn">%s</cite>'), get_comment_author_link()) ?>
		</div>
<?php if ($comment->comment_approved == '0') : ?>
		<em class="comment-awaiting-moderation"><?php _e('Your comment is awaiting moderation.') ?></em>
		<br />
<?php endif; ?>

		<div class="comment-meta commentmetadata"><a href="<?php echo htmlspecialchars( get_comment_link( $comment->comment_ID ) ) ?>">
			<?php
				/* translators: 1: date, 2: time */
				printf( __('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?></a><?php edit_comment_link(__('(Edit)'),'  ','' );
			?>
		</div>

		<?php comment_text() ?>

		<div class="reply">
		<?php comment_reply_link(array_merge( $args, array('add_below' => $add_below, 'depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
		</div>
		<?php if ( 'div' != $args['style'] ) : ?>
		</div>
		<?php endif; ?>
<?php
        }

?>